package com.smarth.zhizulian.http.observer.bad;

import android.content.Context;

import com.smarth.zhizulian.http.observer.inter.BaseObserver;
import com.smarth.zhizulian.utils.DialogUtils;
import com.smarth.zhizulian.view.LoadingLayout;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by 18865 on 2017/12/23.
 * 普通页面访问
 */

public abstract class BadPageObserver<T> extends BaseObserver<T> {
    private LoadingLayout mLoadingLayout;
    private boolean mIsFirst;
    private boolean mIsShowDialog;

    public BadPageObserver(Context context, LoadingLayout loadingLayout, boolean isFirst, boolean isShowDialog) {
        super(context);
        this.mLoadingLayout = loadingLayout;
        this.mIsFirst = isFirst;
        this.mIsShowDialog = isShowDialog;
    }

    public BadPageObserver(Context context, LoadingLayout loadingLayout, boolean isFirst) {
        this(context, loadingLayout, isFirst, true);
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        super.onSubscribe(d);
        if (mIsFirst) {
            mLoadingLayout.showLoading();
        } else {
            if (mIsShowDialog) {
                DialogUtils.showNetWorkAccess(mOContext);
            }
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        super.onError(e);
        mLoadingLayout.showError();
        DialogUtils.closeProgressDialog();
    }

    @Override
    public void onComplete() {
        mLoadingLayout.showContent();
        DialogUtils.closeProgressDialog();
    }

}
